//
//  YSGDepartmentViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/11/17.
//  Copyright © 2020 QDSG. All rights reserved.
//

import Foundation
import HandyJSON

struct YSGDepartmentViewModel {
    let models: [YSGDepartment]
    
    var numberOfRow: Int { models.count }
    
    func viewModel(at indexPath: IndexPath) -> YSGDepartmentItemViewModel {
        return YSGDepartmentItemViewModel(model: models[indexPath.row])
    }
    
    func flowLayout() -> UICollectionViewFlowLayout {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = itemSize()
        flowLayout.minimumLineSpacing = 13
        flowLayout.minimumInteritemSpacing = 13
        return flowLayout
    }
    
    func itemSize() -> CGSize {
        let itemW = (screenW - 16 * 2 - 13 * 3) / 4
        let itemH: CGFloat = 62
        return CGSize(width: itemW, height: itemH)
    }
    
    func cellHeight() -> CGFloat {
        let row = (numberOfRow - 1) / 4 + 1
        return (itemSize().height + 13) * CGFloat(row) + 16 * 2 - 13
    }
    
    /// 获取科室列表 flag: 0-推荐科室 1-全部科室
    static func getDepartmentsList(flag: Int = 1, finished: @escaping ([YSGDepartment], String?, HttpCode) -> Void) {
        let params = ["flag": flag]
        HttpTool.shared.request(target: ApiManager.getDepartmentList(paras: params), success: { (json, code) in
            YSGLog(json)
            if let models = JSONDeserializer<YSGDepartment>.deserializeModelArrayFrom(json: json["data"].description) as? [YSGDepartment] {
                finished(models, nil, code)
            } else {
                finished([], "请求数据发生错误，请重试!", .failure)
            }
        }) { error in
            YSGLog(error)
            finished([], error, .failure)
        }
    }
}

struct YSGDepartmentItemViewModel {
    let model: YSGDepartment
}
